﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HammerMUD.System;
using System.Xml.Serialization;
using System.IO;

namespace HammerMUD.Accounts
{
    public static class NewAccountUtilities
    {
        public static void ProcessNewAccount(string line, User user, List<Account> accounts, HammerMUD.System.HammerMUD hammerMUD)
        {
            switch (user.CurrentState.NewAccount)
            {
                case NewAccountState.Confirmation:
                    Confirmation(line, user);
                    break;
                case NewAccountState.ConfirmPassword:
                    ConfirmPassword(line, user, hammerMUD);
                    break;
                case NewAccountState.EnterName:
                    EnterName(line, user, accounts);
                    break;
                case NewAccountState.EnterPassword:
                    EnterPassword(line, user);
                    break;
                case NewAccountState.NameConfirmation:
                    NameConfirmation(line, user);
                    break;
                default:
                    Console.WriteLine("Error! Invalid new account state!");
                    break;
            }
        }

        public static void Confirmation(string line, User user)
        {
            if (line.Trim().ToLower().Equals("y") || line.Trim().ToLower().Equals("yes"))
            {
                user.CurrentState.NewAccount = NewAccountState.NameConfirmation;
                user.Send("Creating a new account..");
                user.Prompt("The account name is " + user.UserAccount.Name + ". Is that correct?");
            }
            else if (line.Trim().ToLower().Equals("n") || line.Trim().ToLower().Equals("no"))
            {
                user.CurrentState.Base = BaseState.LogIn;
                user.CurrentState.LogIn = LogInState.NotLoggedIn;
                user.Prompt("Your account name is ");
            }
            else
            {
                user.Prompt("Do you wish to create a new account? ");
            }
        }

        public static void NameConfirmation(string line, User user)
        {
            if (line.Trim().ToLower().Equals("y") || line.Trim().ToLower().Equals("yes"))
            {
                user.CurrentState.NewAccount = NewAccountState.EnterPassword;
                user.Prompt("Your password will be ");
            }
            else if (line.Trim().ToLower().Equals("n") || line.Trim().ToLower().Equals("no"))
            {
                user.CurrentState.NewAccount = NewAccountState.EnterName;
                user.Prompt("Your new account name is ");
            }
            else
            {
                user.Prompt("Do you wish to create a new account? ");
            }            
        }

        public static void EnterName(string line, User user, List<Account> accounts)
        {
            foreach (Account a in accounts)
            {
                if (line.Trim().ToLower().Equals(a.Name.ToLower()))
                {
                    user.Send("Account name already in use!");
                    user.Prompt("Your new account name is ");
                }

                user.CurrentState.NewAccount = NewAccountState.NameConfirmation;
                user.Prompt("The account name is " + user.UserAccount.Name + ". Is that correct?");
            }
        }

        public static void EnterPassword(string line, User user)
        {
            user.UserAccount.Password = line.Trim();
            user.CurrentState.NewAccount = NewAccountState.ConfirmPassword;
            user.Prompt("Confirm that your password will be ");
        }

        public static void ConfirmPassword(string line, User user, HammerMUD.System.HammerMUD hammerMUD)
        {
            if (line.Trim().Equals(user.UserAccount.Password))
            {
                user.UserAccount.User = user;
                user.Send("Account " + user.UserAccount.Name + " created!");
                user.Send(AccountMenuUtilities.AccountMenu);
                user.CurrentState.Base = BaseState.AccountMenu;
                user.CurrentState.AccountMenu = AccountMenuState.Menu;
                HammerMUD.System.HammerMUD.Accounts.Add(user.UserAccount);
                user.UserAccount.CustomPrompt = ">> ";
                SystemUtilities.SaveAccounts(HammerMUD.System.HammerMUD.Accounts);
            }
            else
            {
                user.CurrentState.NewAccount = NewAccountState.EnterPassword;
                user.Prompt("Your password will be ");
            }
        }
    }
}
